#  toGeoTIFF v 0.1
#  Copyright (C) 2010 John Donovan - mersey.viking@gmail.com

#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at

#  http://www.apache.org/licenses/LICENSE-2.0

#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import osgeo.gdal as gdal
from osgeo.gdalconst import *

def to_geotiff(img, filename):
    driver = gdal.GetDriverByName('GTiff')
    dataset = driver.Create(filename, img.shape[1], img.shape[0], 1, \
                            GDT_Float32)

    dataset.GetRasterBand(1).SetNoDataValue(img.fill_value)

    # TODO: Add support for geolocation.
    geotransform = [0.0, 1.0, 0.0, img.shape[0], 0.0, -1.0]
    dataset.SetGeoTransform(geotransform)

    dataset.GetRasterBand(1).WriteArray(img, 0, 0)
    
    dataset = None

